#include "db.h"

#include "in.h"

int test_select_records_ufr()
{
	struct MemDatabase		*db = NULL ;
	struct MemTable			*table = NULL ;
	struct MemIndex			*index = NULL ;
	struct UserFriendRelation	user_friend_relat ;
	struct UserFriendRelation	user_friend_relat_array[ 20 ] ;
	size_t				user_friend_relat_array_count ;
	int				i ;
	int				j ;
	
	int				nret = 0 ;
	
	printf( "MDBCreateDatabase ...\n" );
	db = MDBCreateDatabase( "mydb" ) ;
	if( db == NULL )
	{
		printf( "ERROR : MDBCreateDatabase failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateDatabase ok , db[%p]\n" , db );
	}
	
	printf( "MDBCreateTable mytable ...\n" );
	table = MDBCreateTable( db , "mytable" , sizeof(struct UserFriendRelation) ) ;
	if( table == NULL )
	{
		printf( "ERROR : MDBCreateTable mytable failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateTable mytable ok , table[%p]\n" , table );
	}
	
	printf( "MDBCreateIndex myindex ...\n" );
	index = MDBCreateIndex( table , "myindex" , MDB_INDEX , MDBFIELD(struct UserFriendRelation,user_name) , MDBFIELD_NONE ) ;
	if( index == NULL )
	{
		printf( "ERROR : MDBCreateIndex myindex failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBCreateIndex myindex ok , index[%p]\n" , index );
	}
	
	printf( "MDBInsert ...\n" );
	memset( & user_friend_relat , 0x00 , sizeof(struct UserFriendRelation) );
	for( i = 1 ; i <= 10 ; i++ )
	{
		snprintf( user_friend_relat.user_name , sizeof(user_friend_relat.user_name)-1 , "myname-%d" , i );
		for( j = 1 ; j <= 10 ; j++ )
		{
			snprintf( user_friend_relat.friend_name , sizeof(user_friend_relat.friend_name)-1 , "myfriend-%d" , j );
			nret = MDBInsert( table , & user_friend_relat ) ;
			if( nret )
			{
				printf( "MDBInsert user_name[%s] friend_name[%s] failed[%d]\n" , user_friend_relat.user_name , user_friend_relat.friend_name , nret );
				return -1;
			}
			else
			{
				printf( "MDBInsert user_name[%s] friend_name[%s]\n" , user_friend_relat.user_name , user_friend_relat.friend_name );
			}
		}
	}
	printf( "MDBInsert all ok\n" );
	
	printf( "MDBSelect ...\n" );
	memset( & user_friend_relat , 0x00 , sizeof(struct UserFriendRelation) );
	snprintf( user_friend_relat.user_name , sizeof(user_friend_relat.user_name)-1 , "myname-%d" , 7 );
	memset( user_friend_relat_array , 0x00 , sizeof(user_friend_relat_array) );
	nret = MDBSelectBatch( table , user_friend_relat_array , sizeof(user_friend_relat_array)/sizeof(user_friend_relat_array[0]) , & user_friend_relat_array_count , MDBFIELD(struct UserFriendRelation,user_name) , MDBFIELD(struct UserFriendRelation,friend_name) , MDBFIELD_NONE , & user_friend_relat , MDBFIELD(struct UserFriendRelation,user_name) , MDBFIELD_NONE ) ;
	if( nret )
	{
		printf( "ERROR : MDBSelectBatch failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBSelectBatch ok , [%zu]records\n" , user_friend_relat_array_count );
		for( i = 0 ; i < MIN(user_friend_relat_array_count,sizeof(user_friend_relat_array)/sizeof(user_friend_relat_array[0])) ; i++ )
		{
			printf( "    user_name[%s] friend_name[%s]\n" , user_friend_relat_array[i].user_name , user_friend_relat_array[i].friend_name );
		}
	}
	
	printf( "MDBSelect ...\n" );
	memset( & user_friend_relat , 0x00 , sizeof(struct UserFriendRelation) );
	snprintf( user_friend_relat.friend_name , sizeof(user_friend_relat.friend_name)-1 , "myfriend-%d" , 8 );
	memset( user_friend_relat_array , 0x00 , sizeof(user_friend_relat_array) );
	nret = MDBSelectBatch( table , user_friend_relat_array , sizeof(user_friend_relat_array)/sizeof(user_friend_relat_array[0]) , & user_friend_relat_array_count , MDBFIELD(struct UserFriendRelation,user_name) , MDBFIELD(struct UserFriendRelation,friend_name) , MDBFIELD_NONE , & user_friend_relat , MDBFIELD(struct UserFriendRelation,friend_name) , MDBFIELD_NONE ) ;
	if( nret )
	{
		printf( "ERROR : MDBSelectBatch failed[%d]\n" , MDBGetLastError() );
		return -1;
	}
	else
	{
		printf( "MDBSelectBatch ok , [%zu]records\n" , user_friend_relat_array_count );
		for( i = 0 ; i < MIN(user_friend_relat_array_count,sizeof(user_friend_relat_array)/sizeof(user_friend_relat_array[0])) ; i++ )
		{
			printf( "    user_name[%s] friend_name[%s]\n" , user_friend_relat_array[i].user_name , user_friend_relat_array[i].friend_name );
		}
	}
	
	printf( "MDBDropDatabase ...\n" );
	MDBDropDatabase( db );
	printf( "MDBDropDatabase ok\n" );
	
	return 0;
}

int main()
{
	return -test_select_records_ufr();
}

